<!DOCTYPE html>
<html lang="en">
<head>
    <head>
        <meta charset="utf-8">
        <title>oauth2</title>
        <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
</head>
<body>
<div class="col-md-3"></div>
<div class="col-md-6">
<form class="form-horizontal" role="form"  id="login">
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label">账户</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="username"
                   placeholder="请输入账户">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="password"
                   placeholder="请输入密码">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-default" onclick="login()">登录</button>
        </div>
    </div>
</form>
</div>
<div class="col-md-3"></div>


<script type="text/javascript">

    function login() {
        var username = $("#username").val();
        var password = $("#password").val();
        console.log(username)
        console.log(password)
        $.ajax({
            type: "POST",
            url: "http://wonders.liuzhou.gov.cn/sso/remoteLogin",
            data:{
                "username":username,
                "password": password
            },
            success: function (msg) {
                code = msg.code;
                console.log(msg)
                if (code == 200){
                    var tgt = msg.data.tgt
                    getCode(tgt)
                }
            }
        })

    }

    function getCode(tgt) {
        console.log(tgt)
        $.ajax({
            type: "GET",
            url: "/oauth2-demo/getCode",
            data:{
                client_id: "dbb8b982ad154f699805914b0a4c79a5",
                redirect_uri: "http://wonders.liuzhou.gov.cn/budongchan",
                tgt:tgt
            },

            success: function (msg) {
                code = msg.code;
                console.log(msg)
                if (code == 200){
                    var code = msg.data.content.code
                    getUserMsg(code, tgt)
                }
            }
        })
    }

    //获取用户信息
    function getUserMsg(code, tgt) {
        console.log("code---:"+code)
        console.log("tgt--:"+tgt)
        $.ajax({
            url: '/oauth2-demo/getUserMsg',
            method: 'POST',
            data: {
                code: code,
                client_id: "dbb8b982ad154f699805914b0a4c79a5",
                redirect_uri: "http://wonders.liuzhou.gov.cn/budongchan",
                tgt: tgt
            },
            success: function (result) {
                var resultObj = eval('(' + result + ')');
                var type = resultObj.code;
                console.log(resultObj);
                if (type == "200") {
                    var msg = resultObj.data;
                    alert(JSON.stringify(msg));
                    console.log(msg);
                }
            }

        });
    }

</script>

</body>


</html>